using System;
using System.Drawing;
using System.Windows.Forms;

namespace ScreenUp
{

    public partial class SQForm_Selector : Form
    {
        #region:::::::::::::::::::::::::::::::::::::::::::Form level declarations:::::::::::::::::::::::::::::::::::::::::::
        public bool LeftButtonDown = false;
        string ScreenPath;

        public Point ClickPoint = new Point();
        public Point CurrentTopLeft = new Point();
        public Point CurrentBottomRight = new Point();

        Graphics g;
        Pen MyPen = new Pen(Color.Blue, 5);
        
        Pen EraserPen = new Pen(Color.FromArgb(255, 255, 192), 5);

        private Form m_InstanceRef = null;
        public Form InstanceRef
        {
            get
            {
                return m_InstanceRef;
            }
            set
            {
                m_InstanceRef = value;
            }
        }

        #endregion

        #region:::::::::::::::::::::::::::::::::::::::::::Mouse Event Handlers & Drawing Initialization:::::::::::::::::::::::::::::::::::::::::::
        public SQForm_Selector()
        {

            InitializeComponent();
            this.MouseDown += new MouseEventHandler(mouse_Click);
            this.MouseUp += new MouseEventHandler(mouse_Up);
            this.MouseMove += new MouseEventHandler(mouse_Move);
            this.KeyDown += new KeyEventHandler(SQForm_Selector_KeyDown);
            MyPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
            g = this.CreateGraphics();


        }

        void SQForm_Selector_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }

        #endregion

        #region:::::::::::::::::::::::::::::::::::::::::::Exit Button:::::::::::::::::::::::::::::::::::::::::::
        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }
        #endregion
        #region:::::::::::::::::::::::::::::::::::::::::::Mouse Buttons:::::::::::::::::::::::::::::::::::::::::::
        private void mouse_Click(object sender, MouseEventArgs e)
        {
            g.Clear(Color.FromArgb(255, 255, 192));
            LeftButtonDown = true;
            ClickPoint = new Point(System.Windows.Forms.Control.MousePosition.X, System.Windows.Forms.Control.MousePosition.Y);
        }

        private void mouse_Up(object sender, MouseEventArgs e)
        {
            LeftButtonDown = false;
            this.Hide();
            SaveScreen();
            this.Close();

        }
        #endregion

        #region:::::::::::::::::::::::::::::::::::::::::::Drawing the rectangular selection window:::::::::::::::::::::::::::::::::::::::::::
        private void mouse_Move(object sender, MouseEventArgs e)
        {

            //Resize (actually delete then re-draw) the rectangle if the left mouse button is held down
            if (LeftButtonDown)
            {

                //Erase the previous rectangle
                g.DrawRectangle(EraserPen, CurrentTopLeft.X, CurrentTopLeft.Y, CurrentBottomRight.X - CurrentTopLeft.X, CurrentBottomRight.Y - CurrentTopLeft.Y);

                //Calculate X Coordinates
                if (Cursor.Position.X < ClickPoint.X)
                {
                    CurrentTopLeft.X = Cursor.Position.X;
                    CurrentBottomRight.X = ClickPoint.X;
                }
                else
                {
                    CurrentTopLeft.X = ClickPoint.X;
                    CurrentBottomRight.X = Cursor.Position.X;
                }

                //Calculate Y Coordinates
                if (Cursor.Position.Y < ClickPoint.Y)
                {
                    CurrentTopLeft.Y = Cursor.Position.Y;
                    CurrentBottomRight.Y = ClickPoint.Y;
                }
                else
                {
                    CurrentTopLeft.Y = ClickPoint.Y;
                    CurrentBottomRight.Y = Cursor.Position.Y;
                }

                //Draw a new rectangle
                g.DrawRectangle(MyPen, CurrentTopLeft.X, CurrentTopLeft.Y, CurrentBottomRight.X - CurrentTopLeft.X, CurrentBottomRight.Y - CurrentTopLeft.Y);

            }

        }
        #endregion

        #region:::::::::::::::::::::::::::::::::::::::::::SaveScreen:::::::::::::::::::::::::::::::::::::::::::
        private void SaveScreen()
        {

            Size bounds = new Size(CurrentBottomRight.X - CurrentTopLeft.X, CurrentBottomRight.Y - CurrentTopLeft.Y);

            ScreenUpSnapshotter screenshotter = new ScreenUpSnapshotter();
            ScreenUpFTPUploader uploader = new ScreenUpFTPUploader();

            uploader.Upload(screenshotter.GetSnapshot(CurrentTopLeft.X, CurrentTopLeft.Y, bounds), Program.f1.userName.Text, Program.f1.password.Text, 21, Program.f1.host.Text, Program.f1.path.Text, Program.f1.HTTPUrl.Text);
        }
        #endregion

        private void Form1_Load(object sender, EventArgs e)
        {

        }

    }
}